package com.lenovo.smarttraffic.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;
import android.widget.GridLayout;
import android.widget.TextView;

import com.lenovo.smarttraffic.Constant;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.api.onImageClickListener;
import com.lenovo.smarttraffic.ui.adapter.MyTrafficMinusAdapter;
import com.lenovo.smarttraffic.ui.adapter.MyTrafficPlusAdapter;
import com.lenovo.smarttraffic.util.MyItemTouchHelperCallback;

import java.util.ArrayList;
import java.util.Collections;

/**
 * Created by Caleb.F
 * on 2019-12-04
 * at 10:21
 */
public class TrafficItemActivity extends BaseActivity {
    private RecyclerView mRecyclerTop;
    private RecyclerView mRecyclerBottom;
    private TextView mTvDone;

    private ArrayList<String> titleList1;
    private ArrayList<String> titleList2;
    private static final String CURRENT_ITEM = "com.lenovo.smarttraffic.ui.activity.TrafficItemActivity";


    private static final String[] ITEM_STRING = new String[]{
            "推荐", "热点", "科技", "汽车资讯", "健康", "财经", "教育", "旅游", "军事",
            "实时路况", "文化", "二手车", "违章资讯", "娱乐", "体育", "视频", "游戏", "电影"
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        titleList1 = getIntent().getStringArrayListExtra(Constant.EXTRA_ITEM1_TRAFFIC_EXTRA);
        titleList2 = new ArrayList<>();
        initView();
        getData();
        initRecycler();
    }

    private void finishDrag(MyTrafficMinusAdapter myTrafficMinusAdapter) {
        MyItemTouchHelperCallback TouchCallback = new MyItemTouchHelperCallback();
        TouchCallback.setOnItemPositionListener(new MyItemTouchHelperCallback.OnItemPositionListener() {
            @Override
            public void onItemSwap(int from, int target) {
                Collections.swap(titleList1, from, target);
                myTrafficMinusAdapter.notifyItemMoved(from, target);
            }
        });
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(TouchCallback);
        itemTouchHelper.attachToRecyclerView(mRecyclerTop);
    }

    private void initRecycler() {
        mRecyclerBottom.setLayoutManager(new GridLayoutManager(this, 6,
                GridLayout.VERTICAL, false));
        MyTrafficPlusAdapter myTrafficPlusAdapter = new MyTrafficPlusAdapter(this, titleList2);
        mRecyclerBottom.setAdapter(myTrafficPlusAdapter);

        mRecyclerTop.setLayoutManager(new GridLayoutManager(this, 6,
                GridLayout.VERTICAL, false));
        MyTrafficMinusAdapter myTrafficMinusAdapter = new MyTrafficMinusAdapter(this, titleList1);
        mRecyclerTop.setAdapter(myTrafficMinusAdapter);
        initRecyclerListener(myTrafficPlusAdapter, myTrafficMinusAdapter);
        finishDrag(myTrafficMinusAdapter);
    }

    private void initRecyclerListener(MyTrafficPlusAdapter myTrafficPlusAdapter, MyTrafficMinusAdapter myTrafficMinusAdapter) {
        //长按显示
        myTrafficMinusAdapter.setLongClickListener(new MyTrafficMinusAdapter.onLongClickListener() {
            @Override
            public void longClick() {
                myTrafficMinusAdapter.setShowIcon(true);
                myTrafficPlusAdapter.setShowIcon(true);
                //使能textView
                mTvDone.setVisibility(View.VISIBLE);
                mTvDone.setOnClickListener(v -> {
                    Intent intent = new Intent();
                    intent.putStringArrayListExtra(Constant.EXTRA_TRAFFIC_INFO, titleList1);
                    setResult(RESULT_OK, intent);
                    finish();
                });
            }
        });

        //图片点击
        myTrafficPlusAdapter.setOnImageClick(new onImageClickListener() {
            @Override
            public void imageClick(int pos) {
                if(pos!=-1){
                    String remove = titleList2.remove(pos);
                    myTrafficPlusAdapter.notifyItemRemoved(pos);
                    titleList1.add(remove);
                    myTrafficMinusAdapter.notifyItemInserted(myTrafficMinusAdapter.getItemCount());
                }

            }
        });
        //减号图片点击
        myTrafficMinusAdapter.setonImageClick(new onImageClickListener() {
            @Override
            public void imageClick(int pos) {
                if(pos!=-1){
                    String remove = titleList1.remove(pos);
                    myTrafficMinusAdapter.notifyItemRemoved(pos);
                    titleList2.add(remove);
                    myTrafficPlusAdapter.notifyItemInserted(myTrafficPlusAdapter.getItemCount());
                }
            }
        });


    }


    private void getData() {
        for (int i = 0; i < ITEM_STRING.length; i++) {
            if (!titleList1.contains(ITEM_STRING[i])) {
                titleList2.add(ITEM_STRING[i]);
            }
        }
    }

    private void initView() {
        mRecyclerTop = findViewById(R.id.recycler_top);
        mRecyclerBottom = findViewById(R.id.recycler_bottom);
        mTvDone = findViewById(R.id.tv_done);
        initToolBar("添加订阅");
    }


    @Override
    protected int getLayout() {
        return R.layout.activity_traffic_info;
    }
}
